<template>
  <div id="app">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <h3 class="login_title">系统注册</h3>
      <el-form-item label="邮箱" prop="email">
        <el-input type="email" v-model="ruleForm.email" autocomplete="off" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password1" >
        <el-input type="password" v-model="ruleForm.password1" autocomplete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="password2" >
        <el-input type="password" v-model="ruleForm.password2" autocomplete="off" placeholder="请再次输入密码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱验证码" prop="verification" >
        <el-input  v-model="ruleForm.verification" autocomplete="off" placeholder="请输入邮箱验证码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="subCode">发送验证码</el-button>
        <el-button type="primary" @click="subRegist">注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import router from '../router/index'

export default {
  name: 'Regist',
  data () {
    var checkEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else {
        callback()
      }
    }
    var validatePass1 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password1) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        email: '',
        password1: '',
        password2: ''
      },
      rules: {
        email: [
          { validator: checkEmail, trigger: 'blur' }
        ],
        password1: [
          { validator: validatePass1, trigger: 'blur' }
        ],
        password2: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    subCode: function () {
      this.$axios({
        url: `user/code`,
        method: 'post',
        data: {
          email: this.ruleForm.email
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
    },
    subRegist: function () {
      this.$axios({
        url: `user/register`,
        method: 'post',
        data: {
          email: this.ruleForm.email,
          password: this.ruleForm.password2,
          code: this.ruleForm.verification
        }
      }).then(res => {
        // let isRegist = res.status// 这里接收返回的状态码
        if (res.data.data.flag === 200) { // 200可换
          alert(res.data.data.msg)
          router.push({path: '/login'})
        } else {
          console.log(res)
          alert(res.data.data.msg)
        }
      }).catch(err => {
        alert(err)
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.demo-ruleForm {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 400px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

</style>
